package com.mamahao.vmnetframework.test.viewmodel

import android.arch.lifecycle.MutableLiveData
import com.mamahao.vmnetframework.framework.net.Http
import com.mamahao.vmnetframework.framework.net.RequestListener
import com.mamahao.vmnetframework.framework.net.basis.exception.ConnectionException
import com.mamahao.vmnetframework.framework.net.basis.exception.base.BaseException
import com.mamahao.vmnetframework.framework.viewmodel.BaseViewModel
import com.mamahao.vmnetframework.test.model.TestData

class CityViewModel : BaseViewModel(), RequestListener {

    val oneLiveData: MutableLiveData<TestData> by lazy {
        MutableLiveData<TestData>()
    }

    val twoLiveData: MutableLiveData<TestData> by lazy {
        MutableLiveData<TestData>()
    }

    val errorLiveData: MutableLiveData<Boolean> by lazy {
        MutableLiveData<Boolean>()
    }

    //全dsl配置请求方法
    fun queryCityData() {
        Http.call<TestData>(this){
            _method = {
                queryCity("1", "1.1.0")
            }
            _showProgressBar = true
            onFail {
                errorLiveData.value = it is ConnectionException
            }
            onSuccess {
                oneLiveData.value = it
            }
        }
    }

    //把回调放在外部的请求方法
    fun queryCity1Data() {
        Http.call(this,this){
            _method = {
                queryCity("1", "1.1.0")
            }
            _showProgressBar = true
        }
    }

    override fun onStart() {

    }

    override fun onFail(onError: BaseException) {
        errorLiveData.value = onError is ConnectionException
    }

    override fun onSuccess(data: Any) {
        if(data is TestData){
            twoLiveData.value = data
        }
    }

    override fun onFinish() {

    }

}